package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.GroupDetailsBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/9/22 0022.
 */

public class GroupAllMembersAdapter extends BaseRecyclerViewAdapter<GroupDetailsBean.DataBean.NumberBean.UserBean>{

    private Context mContext;
    private onItemClick mListener;

    public GroupAllMembersAdapter(Context context) {
        mContext = context;
    }

    public void setListener(onItemClick listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_all_members, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final GroupDetailsBean.DataBean.NumberBean.UserBean bean = mList.get(position);
            String nickName = bean.getName();
            String avatar = bean.getPhoto();
            ((ViewHolder) holder).mNickname.setText(nickName);
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.default_avatar)
                    .centerCrop()
                    .override(200, 200);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(avatar) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + avatar)
                    .apply(avatarOptions)
                    .into(((ViewHolder) holder).mAvatar);
            ((ViewHolder) holder).itemView.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mListener.OnItemClickListener(holder.getAdapterPosition(), bean);
                }
            });
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_user_avatar)
        CircleImageView mAvatar;
        @BindView(R.id.tv_user_nickname)
        TextView mNickname;

        private View itemView;

        ViewHolder(View view) {
            super(view);
            itemView = view;
            ButterKnife.bind(this, view);
        }
    }

    public interface onItemClick {
        void OnItemClickListener(int position, GroupDetailsBean.DataBean.NumberBean.UserBean bean);
    }
}
